<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>

</head>

<body>
    <div class="bookContainer">
        <h2>图书列表展示</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">图书ID</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>数量</td>
                    <td>定价</td>
                    <td>出版社</td>
                    <td>状态</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody>
                <!-- <tr>
                    <td><input type="checkbox" name="selectBook" value="1" id="selectBook" class="book-select"></td>
                    <td>1</td>
                    <td>大秦帝国第一册</td>
                    <td>我是作者</td>
                    <td>23</td>
                    <td>33.00</td>
                    <td>北京出版社</td>
                    <td>可借阅</td>
                    <td>
                        <div class="op">
                            <a href="book_update.html?bookId=1">修改</a>
                            <a href="javascript:void(0)" onclick="deleteBook(1)">删除</a>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td><input type="checkbox" name="selectBook" value="1" id="selectBook" class="book-select"></td>
                    <td>2</td>
                    <td>大秦帝国第二册</td>
                    <td>我是作者</td>
                    <td>23</td>
                    <td>33.00</td>
                    <td>北京出版社</td>
                    <td>可借阅</td>
                    <td>
                        <div class="op">
                            <a href="book_update.html?bookId=2">修改</a>
                            <a href="javascript:void(0)" onclick="deleteBook(2)">删除</a>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td><input type="checkbox" name="selectBook" value="1" id="selectBook" class="book-select"></td>
                    <td>3</td>
                    <td>大秦帝国第三册</td>
                    <td>我是作者</td>
                    <td>23</td>
                    <td>33.00</td>
                    <td>北京出版社</td>
                    <td>可借阅</td>
                    <td>
                        <div class="op">
                            <a href="book_update.html?bookId=3">修改</a>
                            <a href="javascript:void(0)" onclick="deleteBook(3)">删除</a>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td><input type="checkbox" name="selectBook" value="1" id="selectBook" class="book-select"></td>
                    <td>4</td>
                    <td>大秦帝国第四册</td>
                    <td>我是作者</td>
                    <td>23</td>
                    <td>33.00</td>
                    <td>北京出版社</td>
                    <td>可借阅</td>
                    <td>
                        <div class="op">
                            <a href="book_update.html?bookId=4">修改</a>
                            <a href="javascript:void(0)" onclick="deleteBook(4)">删除</a>
                        </div>
                    </td>
                </tr> -->
            </tbody>
        </table>
        <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div>
        <script>

            getBookList();
            function getBookList() {
                $.ajax({
                    url:"/book/getBookListByPage" + location.search,
                    type:"get",
                    success:function(result) {
                        if (result.data != null && result.data.resultList != null) {
                            finalhtml = ""
                            for(let book of result.data.resultList) {
                                // <!-- 使用单引号，避免拼接时的转义 -->
                                //  <!--不要嵌里面，要拼接-->

                                finalhtml += '<tr>'
                                finalhtml += '<td><input type="checkbox" name="selectBook" value="'+book.id+'" id="selectBook" class="book-select"></td>'
                                finalhtml += '<td>'+book.id+'</td>'
                                finalhtml += '<td>'+book.bookname+'</td>'
                                finalhtml += '<td>'+book.author+'</td>'
                                finalhtml += '<td>'+book.num+'</td>'
                                finalhtml += '<td>'+book.price+'</td>'
                                finalhtml += '<td>'+book.publisher+'</td>'
                                finalhtml += '<td>'+book.statusCN+'</td>'
                                finalhtml += '<td>'
                                finalhtml += '<div class="op">'
                                finalhtml += '<a href="book_update.html?bookId='+book.id+'">修改</a>'
                                finalhtml += '<a href="javascript:void(0)" onclick="deleteBook('+book.id+')">删除</a>'
                                finalhtml += '</div></td></tr>'

                            }
                            $("tbody").html(finalhtml)
                            //翻页信息
                            $("#pageContainer").jqPaginator({
                                totalCounts: result.data.total, //总记录数
                                pageSize: 10,    //每页的个数
                                visiblePages: 5, //可视页数
                                currentPage: result.data.pageRequest.pageNum,  //当前页码
                                first: '<li class="page-item"><a class="page-link">首页</a></li>',
                                prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                                next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                                last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                                page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                                //页面初始化和页码点击时都会执行
                                onPageChange: function (page, type) {
                                    console.log("第"+page+"页, 类型:"+type);
                                    if (type == 'change') {
                                        location.href= "book_list.html?pageNum="+page ;
                                    }
                                }
                            });
                        } else {
                            alert(result.msg)
                        }
                    },
                    error: function (error) {
                        if (error != null && error.status == 401) {
                            location.href = "login.html";
                        }
                    }
                })
            }
    

            function deleteBook(id) {
                var isDelete = confirm("确认删除?");
                if (isDelete) {
                    //删除图书
                    $.ajax({
                        url:"/book/deleteBook",
                        type:"post",
                        data:{"bookId":id},
                        success:function(result) {
                            if (result.code = "SUCCESS" && result.data == "") {
                                location.href = "book_list.html?"+location.search
                                // console.log(location.search)
                                alert("删除成功");
                            }else {
                                alert(result.msg)
                            }
                        },
                        error:function(error) {
                            if (error != null && error.status == 401) {
                                location.href = "login.html";
                            }
                        }
                    })

                }
            }
            function batchDelete() {
                var isDelete = confirm("确认批量删除?");
                if (isDelete) {
                    //获取复选框的id
                    var ids = [];
                    $("input:checkbox[name='selectBook']:checked").each(function () {
                        ids.push($(this).val());
                    });
                    console.log(ids),
                    $.ajax({
                        url: "book/bathDeleteBook",
                        type:"post",
                        data:{
                            ids: ids
                        },
                        success:function(result) {
                            if (result.code = "SUCCESS" && result.data == "") {
                                alert("批量删除成功"),
                                location.href = "book_list.html"
                            }else {
                                alert(result.msg);
                            }

                        },
                        error:function(error) {
                            if (error != null && error.status == 401) {
                                location.href = "login.html";
                            }
                        }
                    })
                }
            }

        </script>
    </div>
</body>

</html>